cap program drop plot_mainf_markups
program define plot_mainf_markups 
	shell mkdir -p "$release/plots/mainf_markups"
	shell mkdir -p "$release/plots_tables/mainf_markups"
	
	use "$sumstats_data_path/harmonized_subs.dta", clear
	gen count = 1
	collapse (sum) p cost count, by(year payer_id best_guess_metal sg_vs_indiv)
	gen markup = p / cost
	gen avg_p = p/count
	gen avg_c = cost/count
	gsort -markup
	export delimited $release/plots_tables/mainf_markups/full.csv, replace
	
	foreach m in 2 3 4 {		
		preserve
		keep if best_guess_metal == `m'
    *Plots
    ** Unweighted
		#delimit ;
		graph box markup, 
			over(sg_vs_indiv) 
			over(year)
			box(1, fcolor("white") lcolor("black")) box(2, color("black"))
			marker(1, mfcolor("white") mlcolor("black")) marker(2,mcolor("black"))
			xsize(2) ysize(2) legend(off) ytitle("medical markup") ylab(0(0.5)4.0, gmax gmin)
			yline(1,lcolor(gray))
			asyvars
		; 
		#delimit cr
		graph export "$release/plots/mainf_markups/`m'.eps", replace 
    ** Weighted
		#delimit ;
		graph box markup [fweight=count], 
			over(sg_vs_indiv) 
			over(year)
			box(1, fcolor("white") lcolor("black")) box(2, color("black"))
			marker(1, mfcolor("white") mlcolor("black")) marker(2,mcolor("black"))
			xsize(2) ysize(2) legend(off) ytitle("medical markup") ylab(0(0.5)4.0, gmax gmin)
			yline(1,lcolor(gray))
			asyvars
		; 
		#delimit cr
		graph export "$release/plots/mainf_markups/w_`m'.eps", replace 
		
		* Tables
    ** Unweighted		
		#delimit ;
		collapse 
			(count) obs = markup   
			(mean)  mean = markup 
			(p50)   p50 = markup  
			(sd)    std_dev = markup, 
			by(sg_vs_indiv year) ;
		#delimit cr
		ren (sg_vs_indiv) (market)
		order market year
		export delimited "$release/plots_tables/mainf_markups/`m'.csv", replace
		
		restore
	
	}

end



